package com.ruoyi.web.controller.knowledge;

import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.law_model.knowledge.domain.DocumentSplit;
import com.ruoyi.law_model.knowledge.domain.Knowledge;
import com.ruoyi.law_model.knowledge.domain.KnowledgeDocuments;
import com.ruoyi.law_model.knowledge.service.IDocumentSplitService;
import com.ruoyi.law_model.knowledge.service.IKnowledgeDocumentsService;
import com.ruoyi.law_model.knowledge.service.IKnowledgeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.page.TableDataInfo;

import java.util.List;

/**
 * 存储知识库Controller
 *
 * @author ruoyi
 * @date 2025-01-09
 */
@RestController
@RequestMapping("/knowledge")
public class KnowledgeController extends BaseController
{
    @Autowired
    private IKnowledgeService knowledgeService;
    @Autowired
    private IKnowledgeDocumentsService knowledgeDocumentsService;
    @Autowired
    private IDocumentSplitService documentSplitService;

    /**
     * 查询存储知识库列表
     */
    @PreAuthorize("@ss.hasPermi('system:knowledge:list')")
    @GetMapping("/list")
    public TableDataInfo list(Knowledge knowledge)
    {
        startPage();
        List<Knowledge> list = knowledgeService.selectKnowledgeList(knowledge);
        return getDataTable(list);
    }

    /**
     * 导出存储知识库列表
     */
    @PreAuthorize("@ss.hasPermi('system:knowledge:export')")
    @Log(title = "存储知识库", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Knowledge knowledge)
    {
        List<Knowledge> list = knowledgeService.selectKnowledgeList(knowledge);
        ExcelUtil<Knowledge> util = new ExcelUtil<Knowledge>(Knowledge.class);
        util.exportExcel(response, list, "存储知识库数据");
    }

    /**
     * 获取存储知识库详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:knowledge:query')")
    @GetMapping(value = "/{klId}")
    public AjaxResult getInfo(@PathVariable("klId") Long klId)
    {
        return success(knowledgeService.selectKnowledgeByKlId(klId));
    }

    /**
     * 新增存储知识库
     */
    @PreAuthorize("@ss.hasPermi('system:knowledge:add')")
    @Log(title = "存储知识库", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Knowledge knowledge)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        knowledge.setCreatedBy(String.valueOf(loginUser.getUserId()));
        knowledge.setUpdatedBy(String.valueOf(loginUser.getUserId()));
        return toAjax(knowledgeService.insertKnowledge(knowledge));
    }

    /**
     * 修改存储知识库
     */
    @PreAuthorize("@ss.hasPermi('system:knowledge:edit')")
    @Log(title = "存储知识库", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Knowledge knowledge)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        knowledge.setUpdatedBy(String.valueOf(loginUser.getUserId()));
        return toAjax(knowledgeService.updateKnowledge(knowledge));
    }

    /**
     * 删除存储知识库
     */
    @PreAuthorize("@ss.hasPermi('system:knowledge:remove')")
    @Log(title = "存储知识库", businessType = BusinessType.DELETE)
    @DeleteMapping("/{klIds}")
    public AjaxResult remove(@PathVariable Long[] klIds)
    {
        //不能批量删除
        if (klIds.length > 1) {
            return AjaxResult.error("不支持批量删除知识库");
        }
        KnowledgeDocuments knowledgeDocuments = new KnowledgeDocuments();
        knowledgeDocuments.setKlId(klIds[0]);
        // 如果知识库下，还存在文档，则不能删除
        if (knowledgeDocumentsService.selectKnowledgeDocumentsList(knowledgeDocuments).size() > 0) {
            return AjaxResult.error("知识库下存在文档，不能删除，请先删除文档！");
        }
        // 如果知识库下，还存在文档分片信息，则不能删除
        DocumentSplit documentSplit = new DocumentSplit();
        documentSplit.setKlId(klIds[0]);
        if (documentSplitService.selectDocumentSplitList(documentSplit).size() > 0) {
            return AjaxResult.error("知识库下存在文档分段数据，不能删除，请先删除文档以及分段数据！");
        }
        return toAjax(knowledgeService.deleteKnowledgeByKlIds(klIds));
    }
}
